﻿using System;
using UnityEngine;

namespace UnityStandardAssets.CinematicEffects
{
	// Token: 0x020004C8 RID: 1224
	[ExecuteInEditMode]
	[RequireComponent(typeof(Camera))]
	[AddComponentMenu("Image Effects/Cinematic/Lens Aberrations")]
	public class LensAberrations : MonoBehaviour
	{
		// Token: 0x17000206 RID: 518
		// (get) Token: 0x0600190F RID: 6415 RVA: 0x000721E1 File Offset: 0x000705E1
		public Shader shader
		{
			get
			{
				if (this.m_Shader == null)
				{
					this.m_Shader = Shader.Find("Hidden/LensAberrations");
				}
				return this.m_Shader;
			}
		}

		// Token: 0x17000207 RID: 519
		// (get) Token: 0x06001910 RID: 6416 RVA: 0x0007220A File Offset: 0x0007060A
		public Material material
		{
			get
			{
				if (this.m_Material == null)
				{
					this.m_Material = ImageEffectHelper.CheckShaderAndCreateMaterial(this.shader);
				}
				return this.m_Material;
			}
		}

		// Token: 0x06001911 RID: 6417 RVA: 0x00072234 File Offset: 0x00070634
		private void OnEnable()
		{
			if (!ImageEffectHelper.IsSupported(this.shader, false, false, this))
			{
				base.enabled = false;
			}
			this.m_RTU = new RenderTextureUtility();
		}

		// Token: 0x06001912 RID: 6418 RVA: 0x0007225B File Offset: 0x0007065B
		private void OnDisable()
		{
			if (this.m_Material != null)
			{
				UnityEngine.Object.DestroyImmediate(this.m_Material);
			}
			this.m_Material = null;
			this.m_RTU.ReleaseAllTemporaryRenderTextures();
		}

		// Token: 0x06001913 RID: 6419 RVA: 0x0007228C File Offset: 0x0007068C
		private void OnRenderImage(RenderTexture source, RenderTexture destination)
		{
			if (!this.vignette.enabled && !this.chromaticAberration.enabled && !this.distortion.enabled)
			{
				Graphics.Blit(source, destination);
				return;
			}
			this.material.shaderKeywords = null;
			if (this.distortion.enabled)
			{
				float val = 1.6f * Math.Max(Mathf.Abs(this.distortion.amount), 1f);
				float num = 0.017453292f * Math.Min(160f, val);
				float y = 2f * Mathf.Tan(num * 0.5f);
				Vector4 vector = new Vector4(this.distortion.centerX, this.distortion.centerY, Mathf.Max(this.distortion.amountX, 0.0001f), Mathf.Max(this.distortion.amountY, 0.0001f));
				Vector3 v = new Vector3((this.distortion.amount < 0f) ? (1f / num) : num, y, 1f / this.distortion.scale);
				this.material.EnableKeyword((this.distortion.amount < 0f) ? "UNDISTORT" : "DISTORT");
				this.material.SetVector("_DistCenterScale", vector);
				this.material.SetVector("_DistAmount", v);
			}
			if (this.chromaticAberration.enabled)
			{
				this.material.EnableKeyword("CHROMATIC_ABERRATION");
				Vector4 vector2 = new Vector4(this.chromaticAberration.color.r, this.chromaticAberration.color.g, this.chromaticAberration.color.b, this.chromaticAberration.amount * 0.001f);
				this.material.SetVector("_ChromaticAberration", vector2);
			}
			if (this.vignette.enabled)
			{
				this.material.SetColor("_VignetteColor", this.vignette.color);
				if (this.vignette.blur > 0f)
				{
					int num2 = source.width / 2;
					int num3 = source.height / 2;
					RenderTexture temporaryRenderTexture = this.m_RTU.GetTemporaryRenderTexture(num2, num3, 0, source.format, FilterMode.Bilinear);
					RenderTexture temporaryRenderTexture2 = this.m_RTU.GetTemporaryRenderTexture(num2, num3, 0, source.format, FilterMode.Bilinear);
					this.material.SetVector("_BlurPass", new Vector2(1f / (float)num2, 0f));
					Graphics.Blit(source, temporaryRenderTexture, this.material, 0);
					if (this.distortion.enabled)
					{
						this.material.DisableKeyword("DISTORT");
						this.material.DisableKeyword("UNDISTORT");
					}
					this.material.SetVector("_BlurPass", new Vector2(0f, 1f / (float)num3));
					Graphics.Blit(temporaryRenderTexture, temporaryRenderTexture2, this.material, 0);
					this.material.SetVector("_BlurPass", new Vector2(1f / (float)num2, 0f));
					Graphics.Blit(temporaryRenderTexture2, temporaryRenderTexture, this.material, 0);
					this.material.SetVector("_BlurPass", new Vector2(0f, 1f / (float)num3));
					Graphics.Blit(temporaryRenderTexture, temporaryRenderTexture2, this.material, 0);
					this.material.SetTexture("_BlurTex", temporaryRenderTexture2);
					this.material.SetFloat("_VignetteBlur", this.vignette.blur * 3f);
					this.material.EnableKeyword("VIGNETTE_BLUR");
					if (this.distortion.enabled)
					{
						this.material.EnableKeyword((this.distortion.amount < 0f) ? "UNDISTORT" : "DISTORT");
					}
				}
				if (this.vignette.desaturate > 0f)
				{
					this.material.EnableKeyword("VIGNETTE_DESAT");
					this.material.SetFloat("_VignetteDesat", 1f - this.vignette.desaturate);
				}
				this.material.SetVector("_VignetteCenter", this.vignette.center);
				if (Mathf.Approximately(this.vignette.roundness, 1f))
				{
					this.material.EnableKeyword("VIGNETTE_CLASSIC");
					this.material.SetVector("_VignetteSettings", new Vector2(this.vignette.intensity, this.vignette.smoothness));
				}
				else
				{
					this.material.EnableKeyword("VIGNETTE_FILMIC");
					float z = (1f - this.vignette.roundness) * 6f + this.vignette.roundness;
					this.material.SetVector("_VignetteSettings", new Vector3(this.vignette.intensity, this.vignette.smoothness, z));
				}
			}
			int pass = 0;
			if (this.vignette.enabled && this.chromaticAberration.enabled && this.distortion.enabled)
			{
				pass = 7;
			}
			else if (this.vignette.enabled && this.chromaticAberration.enabled)
			{
				pass = 5;
			}
			else if (this.vignette.enabled && this.distortion.enabled)
			{
				pass = 6;
			}
			else if (this.chromaticAberration.enabled && this.distortion.enabled)
			{
				pass = 4;
			}
			else if (this.vignette.enabled)
			{
				pass = 3;
			}
			else if (this.chromaticAberration.enabled)
			{
				pass = 1;
			}
			else if (this.distortion.enabled)
			{
				pass = 2;
			}
			Graphics.Blit(source, destination, this.material, pass);
			this.m_RTU.ReleaseAllTemporaryRenderTextures();
		}

		// Token: 0x04000FEA RID: 4074
		[LensAberrations.SettingsGroup]
		public LensAberrations.DistortionSettings distortion = LensAberrations.DistortionSettings.defaultSettings;

		// Token: 0x04000FEB RID: 4075
		[LensAberrations.SettingsGroup]
		public LensAberrations.VignetteSettings vignette = LensAberrations.VignetteSettings.defaultSettings;

		// Token: 0x04000FEC RID: 4076
		[LensAberrations.SettingsGroup]
		public LensAberrations.ChromaticAberrationSettings chromaticAberration = LensAberrations.ChromaticAberrationSettings.defaultSettings;

		// Token: 0x04000FED RID: 4077
		[SerializeField]
		private Shader m_Shader;

		// Token: 0x04000FEE RID: 4078
		private Material m_Material;

		// Token: 0x04000FEF RID: 4079
		private RenderTextureUtility m_RTU;

		// Token: 0x020004C9 RID: 1225
		[AttributeUsage(AttributeTargets.Field)]
		public class SettingsGroup : Attribute
		{
		}

		// Token: 0x020004CA RID: 1226
		[AttributeUsage(AttributeTargets.Field)]
		public class AdvancedSetting : Attribute
		{
		}

		// Token: 0x020004CB RID: 1227
		[Serializable]
		public struct DistortionSettings
		{
			// Token: 0x17000208 RID: 520
			// (get) Token: 0x06001916 RID: 6422 RVA: 0x000728EC File Offset: 0x00070CEC
			public static LensAberrations.DistortionSettings defaultSettings
			{
				get
				{
					return new LensAberrations.DistortionSettings
					{
						enabled = false,
						amount = 0f,
						centerX = 0f,
						centerY = 0f,
						amountX = 1f,
						amountY = 1f,
						scale = 1f
					};
				}
			}

			// Token: 0x04000FF0 RID: 4080
			public bool enabled;

			// Token: 0x04000FF1 RID: 4081
			[Range(-100f, 100f)]
			[Tooltip("Distortion amount.")]
			public float amount;

			// Token: 0x04000FF2 RID: 4082
			[Range(-1f, 1f)]
			[Tooltip("Distortion center point (X axis).")]
			public float centerX;

			// Token: 0x04000FF3 RID: 4083
			[Range(-1f, 1f)]
			[Tooltip("Distortion center point (Y axis).")]
			public float centerY;

			// Token: 0x04000FF4 RID: 4084
			[Range(0f, 1f)]
			[Tooltip("Amount multiplier on X axis. Set it to 0 to disable distortion on this axis.")]
			public float amountX;

			// Token: 0x04000FF5 RID: 4085
			[Range(0f, 1f)]
			[Tooltip("Amount multiplier on Y axis. Set it to 0 to disable distortion on this axis.")]
			public float amountY;

			// Token: 0x04000FF6 RID: 4086
			[Range(0.01f, 5f)]
			[Tooltip("Global screen scaling.")]
			public float scale;
		}

		// Token: 0x020004CC RID: 1228
		[Serializable]
		public struct VignetteSettings
		{
			// Token: 0x17000209 RID: 521
			// (get) Token: 0x06001917 RID: 6423 RVA: 0x00072954 File Offset: 0x00070D54
			public static LensAberrations.VignetteSettings defaultSettings
			{
				get
				{
					return new LensAberrations.VignetteSettings
					{
						enabled = false,
						color = new Color(0f, 0f, 0f, 1f),
						center = new Vector2(0.5f, 0.5f),
						intensity = 1.4f,
						smoothness = 0.8f,
						roundness = 1f,
						blur = 0f,
						desaturate = 0f
					};
				}
			}

			// Token: 0x04000FF7 RID: 4087
			public bool enabled;

			// Token: 0x04000FF8 RID: 4088
			[ColorUsage(false)]
			[Tooltip("Vignette color. Use the alpha channel for transparency.")]
			public Color color;

			// Token: 0x04000FF9 RID: 4089
			[Tooltip("Sets the vignette center point (screen center is [0.5,0.5]).")]
			public Vector2 center;

			// Token: 0x04000FFA RID: 4090
			[Range(0f, 3f)]
			[Tooltip("Amount of vignetting on screen.")]
			public float intensity;

			// Token: 0x04000FFB RID: 4091
			[Range(0.01f, 3f)]
			[Tooltip("Smoothness of the vignette borders.")]
			public float smoothness;

			// Token: 0x04000FFC RID: 4092
			[LensAberrations.AdvancedSetting]
			[Range(0f, 1f)]
			[Tooltip("Lower values will make a square-ish vignette.")]
			public float roundness;

			// Token: 0x04000FFD RID: 4093
			[Range(0f, 1f)]
			[Tooltip("Blurs the corners of the screen. Leave this at 0 to disable it.")]
			public float blur;

			// Token: 0x04000FFE RID: 4094
			[Range(0f, 1f)]
			[Tooltip("Desaturate the corners of the screen. Leave this to 0 to disable it.")]
			public float desaturate;
		}

		// Token: 0x020004CD RID: 1229
		[Serializable]
		public struct ChromaticAberrationSettings
		{
			// Token: 0x1700020A RID: 522
			// (get) Token: 0x06001918 RID: 6424 RVA: 0x000729E4 File Offset: 0x00070DE4
			public static LensAberrations.ChromaticAberrationSettings defaultSettings
			{
				get
				{
					return new LensAberrations.ChromaticAberrationSettings
					{
						enabled = false,
						color = Color.green,
						amount = 0f
					};
				}
			}

			// Token: 0x04000FFF RID: 4095
			public bool enabled;

			// Token: 0x04001000 RID: 4096
			[ColorUsage(false)]
			[Tooltip("Channels to apply chromatic aberration to.")]
			public Color color;

			// Token: 0x04001001 RID: 4097
			[Range(-50f, 50f)]
			[Tooltip("Amount of tangential distortion.")]
			public float amount;
		}

		// Token: 0x020004CE RID: 1230
		private enum Pass
		{
			// Token: 0x04001003 RID: 4099
			BlurPrePass,
			// Token: 0x04001004 RID: 4100
			Chroma,
			// Token: 0x04001005 RID: 4101
			Distort,
			// Token: 0x04001006 RID: 4102
			Vignette,
			// Token: 0x04001007 RID: 4103
			ChromaDistort,
			// Token: 0x04001008 RID: 4104
			ChromaVignette,
			// Token: 0x04001009 RID: 4105
			DistortVignette,
			// Token: 0x0400100A RID: 4106
			ChromaDistortVignette
		}
	}
}
